package org.csu.jpetstore.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.csu.jpetstore.entity.AlipayBean;
import org.csu.jpetstore.entity.AlipayProperties;
import org.csu.jpetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class AlipayUtil {

    @Autowired
    OrderService orderService;
    /**
     * 支付接口
     */
    @Autowired
    AlipayProperties alipayProperties;

    public String pay(AlipayBean alipayBean, String orderId, String subject, String TotalAmount) throws AlipayApiException {
        // 1、获得初始化的AlipayClient
        String serverUrl = alipayProperties.getGatewayUrl();
        String appId = alipayProperties.getApp_id();
        String privateKey = alipayProperties.getPrivateKey();
        String format = "json";
        String charset = alipayProperties.getCharset();
        String alipayPublicKey = alipayProperties.getPublicKey();
        String signType = alipayProperties.getSign_type();
        String returnUrl = alipayProperties.getReturn_url();
        String notifyUrl = alipayProperties.getNotify_url();
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
        // 2、设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        // 页面跳转同步通知页面路径
        alipayRequest.setReturnUrl(returnUrl);
        // 服务器异步通知页面路径
        alipayRequest.setNotifyUrl(notifyUrl);
        //账户订单号
//        String out_trade_no = new Date().getTime() + UUID.randomUUID().toString();
        String out_trade_no = orderId;
        alipayBean.setOut_trade_no(out_trade_no);
        alipayBean.setSubject(subject);
        alipayBean.setTotal_amount(TotalAmount);
        // 封装参数
        alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
        // 3、请求支付宝进行付款，并获取支付结果
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        // 返回付款信息

        return result;
    }

    //订单验证 可获取到订单的响应状态
    public AlipayTradeQueryResponse tradeCheck(String out_trade_no) throws AlipayApiException {
        String serverUrl = alipayProperties.getGatewayUrl();
        String appId = alipayProperties.getApp_id();
        String privateKey = alipayProperties.getPrivateKey();
        String format = "json";
        String charset = alipayProperties.getCharset();
        String alipayPublicKey = alipayProperties.getPublicKey();
        String signType = alipayProperties.getSign_type();
        String returnUrl = alipayProperties.getReturn_url();
        String notifyUrl = alipayProperties.getNotify_url();
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzxX+MVdn/mfNCkeIvvCke/38koG1jEpvBNskoXdh98iVOP2dgdxXY5qMzDTxyMvthX7bzPp+cR0eyPXypXhAG2uTxMrvwF3qOWEMiSOFjBcC7NckR6yano8dDg9nmr6mLj5DLLFzwPq3U6wwfWzY2/Gcdh0SL/349lRC/qqlPxP7d1WUOkAc/HvJp7JmNJtjt1X7G/ceJhMYx2JkWUgaDePED6SGEPIjA9+P0SnTpIVNMae6Jm35XQo0RtmPXCZSuCiO+epRQsDb6tXEqKDms5SeBNTn8WC/GhXUJLFcNw9JOL/YlCw/9zNHogpdEOfxGgAvUj7hZ/NJP9e+PuZdewIDAQAB", signType);
//        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", out_trade_no);
        request.setBizContent(bizContent.toString());
        String body = null;
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response;
    }
}

